import express from "express";
import { formatRequestQuery, formatResponse } from "../../utils/tools";
import { CatService } from "../../services/pet/CatService";
import { uploadMiddleware } from "../middlewares/uploadFile";

const router = express.Router();

router.post("/", async (req, res) => {
  await CatService.add(req.body);
  res.send(formatResponse<null>(null, "操作成功"));
});

router.delete("/:id", async (req, res) => {
  const result = await CatService.delete(req.params.id);
  res.send(formatResponse(result));
});

router.put("/", async (req, res) => {
  const result = await CatService.update(req.body);
  res.send(formatResponse(result));
});

router.get("/page", async (req, res) => {
  const { query, other } = formatRequestQuery(req.query, [
    "chipCode",
    "name",
    "color",
    "sex",
    "brithday",
    "hair",
    "host",
    "homeName",
    "shelfNum",
    "saleUnit",
    "petType",
    "catteryId"
  ]);

  const result = await CatService.findByPage(query.current, query.size, other);
  res.send(formatResponse<typeof result>(result));
});

router.get("/list", async (req: any, res) => {
  const result = await CatService.findByList(req.query, req.token);
  res.send(formatResponse<typeof result>(result));
});

router.get("/treeList", async (req, res) => {
  const { other } = formatRequestQuery(req.body, ["deptName"]);
  const result = await CatService.findByTreeList(other);
  res.send(formatResponse<typeof result>(result));
});

router.get("/getAllHome", async (_req, res) => {
  const result = await CatService.getAllHome();
  res.send(formatResponse(result));
});

router.post("/import", uploadMiddleware(100).single("file"), async (req: any, res) => {
  const resp = await CatService.importData(req.file.path, req.query.petType);

  res.send(formatResponse(`成功导入 ${resp} 条数据`));
});

router.get("/:id", async (req, res) => {
  const result = await CatService.findById(req.params.id);
  res.send(formatResponse(result));
});
export default router;
